/**
 * 七牛 URL
 *
 * @author xiaomi
 */
const urlEncode = encodeURIComponent

const normalizeUrl = function (key, params) {
  let newURL = String(key).trim().replace(rBaseURL, '')

  if (!newURL || rAbsURL.test(newURL)) {
    return key
  }

  // prefix
  newURL = baseURL + newURL

  // params
  if (params) {
    if (typeof params === 'object') {
      params = Object.keys(params).map(k => {
        return urlEncode(k) + '=' + urlEncode(params[k])
      })
        .join('&')
    }

    newURL += newURL.indexOf('?') < 0 ? '?' : '&'
    newURL += params
  }

  return newURL
}

export default function (...args) {
  return normalizeUrl(...args)
}
